<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>应用集</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
    <script type="text/javascript" src="/js/jquery.min.js"></script>
    <script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
    <script src="/js/lay-config.js?v=1.0.4" charset="utf-8"></script>

<style type="text/css">
  {# 设置table每一行的height #}
  .layui-table-cell {
   height: 50px;
   line-height: 50px;
  }
</style>

</head>
<body>
<blockquote class="layui-elem-quote layui-text">
    注:例如添加应用test-app以后,需要在资源的标签中增加appname:test-app,然后才会在资源集合中展现出来
</blockquote>
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="appname" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <button type="submit" class="layui-btn"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>                        
                    </div>
                </form>
            </div>
        </fieldset>

        <table class="layui-table" id="currentTableId" lay-filter="currentTableFilter"></table>
    </div>
</div>
</body>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>               
    </div>
</script>

<script type="text/html" id="currentTableBar">
    <a href="javascript:;" layuimini-content-href="page/xkube/resourceList.html?appname={{d.appname}}" data-title="{{ d.appname }}资源集合" class="layui-btn layui-btn-sm">资源集合</a>
    <a href="javascript:;" layuimini-content-href="page/wiki/list.html?xcolumn={{d.appname}}" data-title="{{ d.appname }}文章" class="layui-btn layui-btn-normal layui-btn-sm" lay-event="wiki">wiki</a>
    <a class="layui-btn layui-btn-normal layui-btn-sm layui-btn-danger" lay-event="delete">删除</a>
</script>
<script>
    layui.use(['form', 'table','miniTab'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;
            miniTab = layui.miniTab,
            miniTab.listen();

        table.render({
            elem: '#currentTableId',
            url: '/xkube/appname/v1/List',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                //{type: "checkbox", width: 50},
                {field: 'id', width:70, title: 'ID', sort: true, hide:true},
                {field: 'appname', title: '名称', sort: true},
                {field: 'createtime', title: '创建时间'},
                {field: 'remarks',edit:true, title: '备注'},
                {title: '操作', minWidth: 200, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [25, 50, 100],
            limit: 25,
            page: true,
            done: function (res, curr, count) {
                console.log(count);
                if (count == 0) {
                    $(".layui-table-main").html('<div class="layui-none">无数据</div>');
                }
            },
            error: function(res,resp) {
              if (resp == "parsererror") {
                layer.msg('请检查是否登录或重新刷新页面',{icon:2});
              }
            }
        });

      	//监听单元格编辑
      	table.on('edit(currentTableFilter)', function(obj){
        	var value = obj.value //得到修改后的值
        	,datas = obj.data //得到所在行所有键值
        	,field = obj.field; //得到字段
          //console.log(datas);
          var bodystr = {"id":datas.id,"name":field,"value":value};
            layer.confirm('确定修改?', {icon: 3, title:'提示',yes: function(index){ 
             	$.ajax({
                 	url: "/xkube/appname/v1/Edit?id="+datas.id,
                  type: "POST",
                  data: JSON.stringify(bodystr),
                  dataType: "json",
                 	success: function (resp) {
                     	if(resp.code == 0){
                        	layer.msg('['+ datas.id +'] ' + field + '更改为:'+ value);
                     	}else{
                        	layer.msg('['+ datas.id +'] ' + field + '更改失败',{icon:2});
                     	}
                	}
            	});	
            },
            cancel: function(index, layero){ 
                  layer.close(index);
            } 
            });	
      	});

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            //var result = JSON.stringify(data.field);
            data.field.appname = data.field.appname.replace(/^\s*|\s*$/g,"");
            table.reload('currentTableId', {
                //page: {
                //    curr: 1,
                //    limit:50
                //},
                method:'POST',
                url: '/xkube/appname/v1/List',
                //contentType: 'application/json',
                where: data.field,
                done: function(res, curr, count){}
            }, 'data');
            return false;
        });


        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['45%', '85%'],
                    content: '/page/xkube/appnameAdd.html',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
        });

        //监听表格复选框选择
        //table.on('checkbox(currentTableFilter)', function (obj) {
         //   console.log(obj)
        //});

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'delete') {
                layer.confirm('确定删除'+data.appname+'?', {icon: 3, title:'删除提示',yes: function(index){
                     var index2 = layer.load(0, {shade: false});
                     layer.msg('此处需运行1-2s左右');
                			 $.ajax({
                			    url: '/xkube/appname/v1/Del?id='+data.id,
                			    type: "get",
                			    success: function (resp) {
                            layer.close(index2);
                						 if(resp.code == 0){
                							 layer.msg('删除成功', {icon: 1});
                							 window.location.reload();
                						 }else{
                							 layer.msg(resp.msg,{icon:2});
                						 }
                				  }
                		   });		  	  
                },
            		  cancel: function(index, layero){ 
            			    layer.close(index);
                      layer.close(index2);
            			    console.log("不操作");
            		  } 
        		    });             
            }
        });
    });
</script>
</html>